package com.gdut.simple.view.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.data.entity.Favorites;

import java.util.List;

public class FavoritesAdapter extends BaseExpandableListAdapter {

    private List<String> mTypes;
    private List<List<Favorites>> mData;
    private LayoutInflater mInflater;

    public FavoritesAdapter(Context context, List<String> types, List<List<Favorites>> data) {
        mInflater = LayoutInflater.from(context);
        this.mTypes = types;
        this.mData = data;
    }

    @Override
    public int getGroupCount() {
        return mTypes != null ? mTypes.size() : 0;
    }

    @Override
    public int getChildrenCount(int i) {
        return mData != null ? mData.get(i).size() : 0;
    }

    @Override
    public Object getGroup(int i) {
        return mTypes.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return mData.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        GroupHolder holder;
        if (view == null) {
            holder = new GroupHolder();
            view = mInflater.inflate(R.layout.layout_favorites_group_item, viewGroup, false);
            holder.type = (TextView) view.findViewById(R.id.id_favorites_type);
            view.setTag(holder);
        } else {
            holder = (GroupHolder) view.getTag();
        }
        holder.type.setText(mTypes.get(i));
        if (b) {
            holder.type.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_down_arrow, 0);
        } else {
            holder.type.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_right_arrow, 0);
        }
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ChildHolder holder;
        if (view == null) {
            holder = new ChildHolder();
            view = mInflater.inflate(R.layout.layout_favorites_child_item, viewGroup, false);
            holder.title = (TextView) view.findViewById(R.id.id_favorite_title);
            holder.time = (TextView) view.findViewById(R.id.id_favorites_time);
            view.setTag(holder);
        } else {
            holder = (ChildHolder) view.getTag();
        }
        holder.title.setText(mData.get(i).get(i1).getTitle());
        holder.time.setText(mData.get(i).get(i1).getCreatedAt().substring(0, 10));
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private static class GroupHolder {
        TextView type;
    }

    private static class ChildHolder {
        TextView title;
        TextView time;
    }

}
